<?php

namespace Home\Controller;
use Think\Controller;
use Think\Model;

class BroadcastController extends AuthController {
    
    public function index(){
        $this->CheckPower('1001');
        $this->display();
    }

    //客服
    public function customer(){
        if(IS_GET && isset($_GET['id']) && !empty($_GET['id'])){
            $useraccount = M('','','DB_CONFIG2');
            $id = $_GET['id'];
            $d['remsg'] =  $_GET['text'];
            $d['state'] =  2;
            $d['retime'] =  date("Y-m-d H:i:s",time());
            $s = $useraccount->table('game_problem')->where("id=$id")->save($d);
            $data['code'] = 0;
            $data['msg'] = '操作失败';
            if($s){

                $commands = M('','','DB_CONFIG1');
                $serverID = C('DATA_HOST');
                $id_int = intval($_GET['gameid']);
                $command=array(
                    'gameid'=>$id_int,
                    'title'=>'客服反馈',
                    'content'=>$_GET['text']
                );
                $command = json_encode($command);
                $rpcname="sendmail";
                $time=date("Y-m-d H:i:s",time());
                $add['ServerIPAddr'] =$serverID;
                $add['Command'] =$command;
                $add['RpcName'] =$rpcname;
                $add['Time'] =$time;
                $result=  $commands->table('data_command')->add($add);
                $data['code'] = 1;
                $data['msg'] ='操作成功';
            }
            $this->ajaxReturn($data);
        }

        $this->CheckPower('9200');
        $this->display();
    }

    public function ycustomer(){
        $this->CheckPower('9200');
        $this->display('');
    }

    //忽略反馈的内容
    public function delete(){
        $useraccount = M('','','DB_CONFIG2');
        $id = $_GET['id'];
        $d['remsg'] =  '已忽略';
        $d['state'] =  3;
        $d['retime'] =  date("Y-m-d H:i:s",time());
        $s =$useraccount->table('game_problem')->where("id in ($id)")->save($d);
        $data['code'] = 0;
        $data['msg'] = '操作失败';
        if($s){
            $data['code'] = 1;
            $data['msg'] ='操作成功';
        }
        $this->ajaxReturn($data);
    }


    //获取反馈内容信息
    public function getcustomer(){
        $useraccount = M('','','DB_CONFIG2');
        $url = pageurl(__SELF__);
        $pagesize = empty($url['limit'])?10:$url['limit'];
        $page = empty($url['page'])?1:$url['page'];
        $state = ($page-1)*$pagesize;
        if($url['gameid'] > 0 && !empty($url['gameid'])){
            $where['gameid'] = array('eq',$url['gameid']);
            $id = $url['gameid'];
            $this-> actionLog($id,'查询了accountid为','反馈内容');
        }

        if($url['state']=='1'){
            $where['state'] =array('eq',1);
        }else{
            $where['state'] =array('in','2,3');
        }
        $count=$useraccount->table('game_problem')->where($where)->count();
        $list = $useraccount->table('game_problem')->where($where)->order('time desc')->limit($state,$pagesize)->select();



        $data['code'] = 0;
        $data['message'] = $where;
        $data['count'] = $count;
        $data['data'] = $list;
        $this->ajaxReturn($data,'json');
    }

    public function  send(){
        $command = M('','','DB_CONFIG1');
        $content=$_POST['content'];
        $contents=$_POST['content'];
        $content=trim($content);
        if(!$content){
            return;
        }
        $ar=new \stdClass();
        $ar->content=$content;
        $ar->count=(int)($_POST['count']);
        $content=json_encode($ar,JSON_UNESCAPED_UNICODE);
        $serverID = C('DATA_HOST');
        $time=date("Y-m-d H:i:s",time());
        $data['ServerIPAddr'] =$serverID;
        $data['Command'] =$content;
        $data['RpcName'] ='broadcast';
        $data['Time'] =$time;
        $list=  $command->table('data_command')->add($data);
       
        $r=$this->actionLog('公告','发布了','信息',$contents);
        if($list){
             $this->ajaxReturn('发送成功'); 
        }else{
             $this->ajaxReturn('发送失败'); 
        }
    }
    //发布公告
    public function noticeadd(){
       if(IS_POST){

           $add['title'] =I('post.title');
           $add['content'] =I('post.content');
           $add['sender'] ="系统";
           $add['sendtime'] =date("Y-m-d H:i:s");;
           $add['sort'] =I('post.sort');
           $commands = M('','','DB_CONFIG1');
           $re = $commands->table('cfg_notice')->add($add);
           $r=$this->actionLog($_SESSION['ltq_game_admin_user']['name'],'管理员姓名为:','发布了公告');
           $data['code'] = 0;
           $data['msg'] = '发布失败';
           if ($re) {
               $data['code'] = 1;
               $data['msg'] = '发布成功';
           }
           $this->ajaxReturn($data);
       }
       $this->CheckPower('1201');
       $this->display('add');
    }
    //公告修改
    public function noticeupdate(){
        $commands = M('','','DB_CONFIG1');
        if(IS_POST){
          //  $time=date("Y-m-d H:i:s",time());
            $id = $_POST['id'];
            $add['title'] =I('post.title');
            $add['content'] = $_POST['content'];
            $add['sender'] ="系统";
           // $add['sendtime'] =$time;
            $add['sort'] =I('post.sort');
            $re = $commands->table('cfg_notice')->where("id=$id")->save($add);
            $r=$this->actionLog($_SESSION['ltq_game_admin_user']['name'],'管理员姓名为:','编辑了公告');
            $data['code'] = 0;
            $data['msg'] = '操作失败';
            if ($re) {
                $data['code'] = 1;
                $data['msg'] = '操作成功';
            }
            $this->ajaxReturn($data);
        }
        $this->CheckPower('1201');
        $id = $_GET['id'];
        $data = $commands->table('cfg_notice')->where("id=$id")->find();
      //  $s = $commands->table('cfg_notice')->fetchSql(true)->where("id=$id")->find();
       // print_r($data);
      //  echo $s;
        $this->assign('lists',$data);
        $this->display();
    }


    //公告删除
    public function noticedelete(){
        $commands = M('','','DB_CONFIG1');
        $id = $_GET['id'];
        $re = $commands->table('cfg_notice')->delete($id);
        $data['code'] = 0;
        $data['msg'] = '操作失败';
        if ($re) {
            $data['code'] = 1;
            $data['msg'] = '操作成功';
        }
        $this->ajaxReturn($data);
    }

    public function gamenotice(){
        $this->display();
    }

    ///操作记录
    public function czlogs(){
        $this->CheckPower('9004');
        $pageSize=25;
        $p = $_GET['p'];
        $time = $_GET['time'];
        $condi['start_time'] =$_GET['start_time'];
        $condi['end_time'] =$_GET['end_time'];
        $where =' where 0=0 ';
        if($condi['start_time']){
            $sa=$condi["start_time"];
            $where .=" and SUBSTR(create_time,1,10)>='$sa'";
        }
        if($condi['end_time']){
            $end=$condi['end_time'];
            $where .=" and SUBSTR(create_time,1,10)<='$end'";
        }
        $p = $p ? $p : 1;         //第几页
        $page=((int)$p-1)*(int)$pageSize;
        $count=$this->weblog("SELECT count(*) from data_logs $where");
        // var_dump("SELECT user_id,param_name,create_time,content from data_logs $where order by create_time desc limit $page,$pageSize ");
        $result=$this->weblog("SELECT user_id,param_name,create_time,content from data_logs $where order by create_time desc limit $page,$pageSize ");
        foreach ($result as $key => $value) {
            $result[$key]['username']= $this->Getusername($value['user_id']);
        }
        $show = $this->page_show($count,$pageSize,$p);
        $shows=$show;
        $pg=array_pop($shows);
        $this->assign('count', reset(reset($count)));
        // $this->assign("page_show", $show); //分页按钮组
        $this->assign("p", $p); //分页按钮组
        $this->assign('totalPages', $pg);
        $this->assign("condi", $condi); //分页按钮组
        $this->assign('lists',$result);
        $this->display();
    }
    //扣除金币记录
    public function money_logs(){
        $this->CheckPower('9006');
        $pageSize=25;
        $p = $_GET['p'];
        $time = $_GET['time'];
        $condi['start_time'] =$_GET['start_time'];
        $condi['end_time'] =$_GET['end_time'];
        $where =' where 0=0 ';
        if($condi['start_time']){
            $sa=$condi["start_time"];
            $where .=" and datetime>'$sa'";
        }
        if($condi['end_time']){
            $end=$condi['end_time'];
            $where .=" and datetime<'$end'";
        }
        $p = $p ? $p : 1;         //第几页
        $page=((int)$p-1)*(int)$pageSize;
        $count=$this->weblog("SELECT count(*) from data_kcmoney $where");
        $result=$this->weblog("SELECT accountid,datetime,kcjb,operator from data_kcmoney $where order by datetime desc limit $page,$pageSize ");
        foreach ($result as $key => $value) {
            $result[$key]['username']= $this->Getplayername($value['accountid']);
        }
        $show = $this->page_show($count,$pageSize,$p);
        $shows=$show;
        $pg=array_pop($shows);
        $this->assign('totalPages', $pg);
        $this->assign('count', reset(reset($count)));
        // $this->assign("page_show", $show); //分页按钮组
        $this->assign("p", $p); //分页按钮组
        $this->assign("condi", $condi); //分页按钮组
        $this->assign('lists',$result);
        $this->display();
    }
    //登陆日志
    public function login_logs(){
        $this->CheckPower('9005');
        $pageSize=25;
        $p = $_GET['p'];
        $time = $_GET['time'];
        $condi['start_time'] =$_GET['start_time'];
        $condi['end_time'] =$_GET['end_time'];
        $where =' where 0=0 ';
        if($condi['start_time']){
            $sa=$condi["start_time"];
            $where .=" and SUBSTR(logtime,1,10)>'$sa'"; 
        }
        if($condi['end_time']){
            $end=$condi['end_time'];
            $where .=" and SUBSTR(logtime,1,10)<'$end'";
        }
        $p = $p ? $p : 1;         //第几页
        $page=((int)$p-1)*(int)$pageSize;
        $count=$this->weblog("SELECT count(*) from data_login_logs");
        $result=$this->weblog("SELECT operator,logtime,ip from data_login_logs $where order by logtime   desc  limit $page,$pageSize");
        $show = $this->page_show($count,$pageSize,$p);
        $shows=$show;
        $pg=array_pop($shows);
        $this->assign('totalPages', $pg);
        $this->assign('count', reset(reset($count)));
        // $this->assign("page_show", $show); //分页按钮组
        $this->assign("p", $p); //分页按钮组
        $this->assign("condi", $condi); //分页按钮组
        $this->assign('lists',$result);
        $this->display();
    }



}
?>